package com.sodaopen.study.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sodaopen.study.entity.ApiResult;
import com.sodaopen.study.entity.User;
import com.sodaopen.study.service.UserService;

@Controller
public class StudentController {


    @Autowired
    private UserService userService;
    
    @GetMapping("teach/student")
    public String list(){
        return "teach/student";
    }

    @GetMapping("/api/student/list")
    @ResponseBody
    public Object listCourse(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "15") Integer limit){

        ApiResult result = new ApiResult();
        try{
            Page<User> pageContent = PageHelper.startPage(page, limit);
            List<User> list = userService.findByParam();
            result.setCode(0);
            result.setCount(pageContent.getTotal());
            result.setData(list);
        }catch (Exception e){
            result.setCode(1);
            result.setMsg("数据请求失败");
        }
        return result;
    }
    
    
    @PostMapping("/api/student/changeClazz")
    @ResponseBody
    public Object changeClazz(Integer userId, Integer clazzId){

        ApiResult result = new ApiResult();
        try{
            userService.updateUserClazz(userId, clazzId);
            result.setCode(0);
            result.setMsg("修改成功");
        }catch (Exception e){
            result.setCode(1);
            result.setMsg("修改失败");
            e.printStackTrace();
        }
        return result;
    }

    
}
